
# Note: this file was automatically converted to Python from the
# original steve-language source code.      Please see the original
# file for more detailed comments and documentation.

import breve
import math

PI = 3.14

agentpile = 0

class Controller( breve.Control ):
    def __init__( self ):
        breve.Control.__init__( self )
        Controller.init( self )
    
    def init( self ):
        wall = breve.createInstances( breve.Stationary, 1 )
        wall.register( breve.createInstances( breve.Cube, 1 ).initWith( breve.vector(90,1,5) ), breve.vector(55,0,-2) )
        wall2 = breve.createInstances( breve.Stationary, 1 )
        wall2.register( breve.createInstances( breve.Cube, 1 ).initWith( breve.vector(100,1,5) ), breve.vector(50,100,-2) )
        wall3 = breve.createInstances( breve.Stationary, 1 )
        wall3.register( breve.createInstances( breve.Cube, 1 ).initWith( breve.vector(1,100,5) ), breve.vector(0,50,-2) )
        wall4 = breve.createInstances( breve.Stationary, 1 )
        wall4.register( breve.createInstances( breve.Cube, 1 ).initWith( breve.vector(1,100,5) ), breve.vector(100,50,-2) )
        
        floor = breve.createInstances( breve.Stationary, 1 )
        floor.register( breve.createInstances( breve.Cube, 1 ).initWith( breve.vector(100,100,1) ), breve.vector(50,50,-3) )
        floor.setTextureImage(0)
        floor.setColor(breve.vector(.1,.1,.1))
        
        self.setBackgroundColor( breve.vector( 0, 0, 0 ) )
        self.enableLighting()
        self.enableShadowVolumes()
        self.pointCamera( breve.vector( 50, 40, 0 ), breve.vector( 0, -90, 100 ) )
        # self.setLightColor( breve.vector( 0.200000, 0.200000, 0.500000 ) )
        self.moveLight( breve.vector( 0, 0, 100 ) )
        breve.createInstances(Agent,10)
        # x = breve.createInstances(Agent, 1)
        # x.setVelocity(breve.vector(0,0,0))
        # x.move(breve.vector(50,50,2))
        

class Agent(breve.Mobile):
    def __init__( self ):
        breve.Mobile.__init__( self )
        Agent.init( self )
    
    def collideStationary( self, thing ):
        self.move(self.getLocation() - self.getVelocity().normalize()*3)
        self.setVelocity(breve.randomExpression(breve.vector(25, 25, 0)) - breve.vector(12, 12, 0))
        angle = breve.breveInternalFunctionFinder.angle(self, breve.vector(0,1,0), self.getVelocity().normalize())
        if self.getVelocity().normalize()[0] > 0:
            angle = 2*PI - angle
        self.setRotation(breve.vector(0,0,1), angle)
    
    def collideAgent(self, agent):
        print "death, distance was: ", (self.getLocation() - agent.getLocation()).length()
        global agentpile
        self.alive = False
        self.move(breve.vector(agentpile,-5,0))
        agentpile += 5
        self.setVelocity(breve.vector(0,0,0))

    def iterate(self):
        if self.alive:
            self.setColor(breve.vector(0,1,0))
            if self.mysensor.sense(self):
                self.setColor(breve.vector(1,0,0))
                # self.setVelocity(breve.vector(0,0,0) - self.getVelocity())
                self.setVelocity(breve.randomExpression(breve.vector(25, 25, 0)) - breve.vector(12, 12, 0))
                angle = breve.breveInternalFunctionFinder.angle(self, breve.vector(0,1,0), self.getVelocity().normalize())
                if self.getVelocity().normalize()[0] > 0:
                    angle = 2*PI - angle
                self.setRotation(breve.vector(0,0,1), angle)
            breve.Mobile.iterate(self)

    def init( self ):
        self.setShape(breve.createInstances( breve.Shape, 1 ).initWithPolygonCone(5, 2, 1))
        self.handleCollisions('Stationary', 'collideStationary')
        self.handleCollisions('Agent', 'collideAgent')
        self.setVelocity(breve.randomExpression(breve.vector(25, 25, 0)) - breve.vector(12, 12, 0))
        angle = breve.breveInternalFunctionFinder.angle(self, breve.vector(0,1,0), self.getVelocity().normalize())
        if self.getVelocity().normalize()[0] > 0:
            angle = 2*PI - angle
        self.setRotation(breve.vector(0,0,1), angle)
        self.move(breve.randomExpression(breve.vector(95, 95, 0)) + breve.vector(2,2,0))
        self.setColor(breve.vector(0,1,0))
        self.mysensor = Sensor()
        self.alive = True

class Sensor(breve.Object):
    def __init__(self):
        breve.Object.__init__(self)
    
    def sense(self, obj):
        my_direction = ( obj.getRotation() * breve.vector(0,1,0) )
        
        for i in breve.allInstances(Agent):
            if obj == i or i.alive == False:
                continue
            
            i_direction = i.getLocation() - obj.getLocation()
            angle = breve.breveInternalFunctionFinder.angle(self, i_direction, my_direction)
            
            if (angle <= PI):
                obj.setColor(breve.vector(0,0,1))
                
                if i_direction.length() < 10:
                    return True
                # if ( self.activationMethod and self.activationObject ):
                #     strength = self.activationObject.callMethod( self.activationMethod, [ strength ] )
        
        return False

# Create an instance of our controller object to initialize the simulation
breve.Controller = Controller
Controller()


